package phoneDataComparable;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

// 1 实现writable接口
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class FlowBean implements WritableComparable<FlowBean> {

    private long upFlow;
    private long downFlow;
    private long sumFlow;


    public void set(long upFlow, long downFlow) {
        this.upFlow = upFlow;
        this.downFlow = downFlow;
        this.sumFlow = upFlow + downFlow;
    }

    /**
     * 比较方法
     *
     * @param o
     * @return
     */
    @Override
    public int compareTo(FlowBean o) {
        //降序是-1,升序是1
        return this.sumFlow > o.getSumFlow() ? -1 : 1;
    }

    /**
     * 序列化
     *
     * @param dataOutput
     * @throws IOException
     */
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeLong(upFlow);
        out.writeLong(downFlow);
        out.writeLong(sumFlow);
    }

    /**
     * 反序列化
     *
     * @param dataInput
     * @throws IOException
     */
    @Override
    public void readFields(DataInput in) throws IOException {
        this.upFlow = in.readLong();
        this.downFlow = in.readLong();
        this.sumFlow = in.readLong();
    }

    @Override
    public String toString() {
        return upFlow + "\t" + downFlow + "\t" + sumFlow;
    }

}
//    public FlowBean() {
//    }
//
//    public FlowBean(long upFlow, long downFlow) {
//        this.upFlow = upFlow;
//        this.downFlow = downFlow;
//        this.sumFlow = upFlow + downFlow;
//    }
//
//

//
//    /**
//     * 如果指定的数等于参数返回0
//     * 如果指定的数小于参数返回-1
//            * 如果指定的数大于参数返回1
//     *
//             * @param o
//     * @return
//             */
//    @Override
//    public int compareTo(FlowBean o) {
//        return this.sumFlow > o.getSumFlow() ? -1 : 1;
//    }
//
//    @Override
//    public String toString() {
//        return upFlow + "\t" + downFlow + "\t" + sumFlow;
//    }


